#include "functor.hh"

#include "../server.hh"
#include "../client.hh"

#include <request/type.hh>

Functor::Functor ()
{}

// Display
DisplayFunctor::DisplayFunctor ()
{
  // ici on peux send une balise begin_obj_transfert
}

DisplayFunctor::~DisplayFunctor ()
{
  // ici on peux send une balise end_obj_transfert
}

void
DisplayFunctor::operator() (List<Object*>* list)
{
  if (list)
    {
      // traitement du noeud courant.
    }
}

// Physic
PhysicFunctor::PhysicFunctor ()
  : object_ (0)
{
  intersect_list_ = new std::list<Object*>;
}


PhysicFunctor::~PhysicFunctor ()
{}

void
PhysicFunctor::operator() (List<Object*>* list)
{
  List<Object*>* parc = list;
  while (parc)
  {
    if (object_->intersect (parc->elt))
      intersect_list_->push_back (parc->elt);
    parc = parc->next;
  }
}

void
PhysicFunctor::operator() (std::list<Object*>& list)
{
  std::list<Object*>::iterator it;

  for (it = list.begin (); it != list.end (); ++it)
    if (object_->intersect (*it))
      intersect_list_->push_back (*it);
}

Object*
PhysicFunctor::object_get ()
{
  return object_;
}

void
PhysicFunctor::object_set (Object* obj)
{
  object_ = obj;
  intersect_list_->clear ();
}

// Map
MapFunctor::MapFunctor (Client* client):
  client_ (client)
{
}

MapFunctor::~MapFunctor ()
{
  //   tbb::concurrent_hash_map<int, boost::shared_ptr<Client> >::const_accessor acc;
  //   s_->clientlist ()->find (acc, clientid_);
}

void
MapFunctor::operator() (unsigned char* str)
{
  std::string to_send = "";

  to_send += str[0];
  to_send += str[1];

  client_->nm ()->raw_send (to_send);
}

void
MapFunctor::operator() ()
{
  unsigned char c = 1;
  std::string to_send = "";
  to_send += c;
  to_send += c;

  client_->nm ()->raw_send (to_send);
}

GetObjectFunctor::GetObjectFunctor (Server* s, int clientid)
{
  tbb::concurrent_hash_map<int, boost::shared_ptr<Client> >::const_accessor acc;
  s->clientlist ()->find (acc, clientid);

  client_ = acc->second;
}

GetObjectFunctor::~GetObjectFunctor ()
{}

void
GetObjectFunctor::operator() (List<Object*>* list)
{
  List<Object*>* parc = list;

  while (parc)
  {
    client_->nm ()->send (RType::GET_OBJECT, parc->elt->tonet ());
    parc = parc->next;
  }
}
